<?php
/*
 *      _______ _     _       _     _____ __  __ ______
 *     |__   __| |   (_)     | |   / ____|  \/  |  ____|
 *        | |  | |__  _ _ __ | | _| |    | \  / | |__
 *        | |  | '_ \| | '_ \| |/ / |    | |\/| |  __|
 *        | |  | | | | | | | |   <| |____| |  | | |
 *        |_|  |_| |_|_|_| |_|_|\_\\_____|_|  |_|_|
 */
/*
 *     _________  ___  ___  ___  ________   ___  __    ________  _____ ______   ________
 *    |\___   ___\\  \|\  \|\  \|\   ___  \|\  \|\  \ |\   ____\|\   _ \  _   \|\  _____\
 *    \|___ \  \_\ \  \\\  \ \  \ \  \\ \  \ \  \/  /|\ \  \___|\ \  \\\__\ \  \ \  \__/
 *         \ \  \ \ \   __  \ \  \ \  \\ \  \ \   ___  \ \  \    \ \  \\|__| \  \ \   __\
 *          \ \  \ \ \  \ \  \ \  \ \  \\ \  \ \  \\ \  \ \  \____\ \  \    \ \  \ \  \_|
 *           \ \__\ \ \__\ \__\ \__\ \__\\ \__\ \__\\ \__\ \_______\ \__\    \ \__\ \__\
 *            \|__|  \|__|\|__|\|__|\|__| \|__|\|__| \|__|\|_______|\|__|     \|__|\|__|
 */
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Portal\Controller;
use Common\Controller\HomebaseController; 
//use Vendor\Oauth;
/**
 * 首页
 */
class DemoController extends HomebaseController {



	public function index()
	{
		Vendor('jssdk.jssdks');
		$a = new \jssdk();
		$signPackage = $a -> getSignPackage();
		 $this->assign('signPackage', $signPackage); 
		 // pr($signPackage); 
		$this -> display();
	}
	
	public function wxupload() {  
$img_str = I('img_str');  

        // $uploadROOT     = realpath(THINK_PATH.'../ceshi/');//定义保存路径       
// $uploadPath     = $uploadROOT."/Upload/".date('Y-m-d')."/";//为方便管理图片 保存图片时 已时间作一层目录作区分  
// $uploadPath     ="./Upload/".date('Y-m-d')."/";//为方便管理图片 保存图片时 已时间作一层目录作区分  
// if(!file_exists($uploadPath)) mkdir($uploadPath,  775);  
              
// $savepath = './Upload/' . date('Y-m-d').'/';   
$img_str = rtrim($img_str, ',');  
   
$img_arr = explode(',', $img_str);  

$str = ''; //sql语句字符串  
$imgs = array();  
  
foreach($img_arr as $v) {  
    $imgs[] = $this->doWechatPic($v);  
}  
  
// $str = ''; //sql语句字符串  
// foreach($imgs as $v) {  
//     $str .= "(NULL," . $this->_user_info_id . "," . $publish_id . ",'" . $v . "')" . ",";  
// }  
// $sql = "INSERT INTO img(`id`,`userid`,`publish_id`,`img_url`) values" . rtrim($str, ',');  
// $res = M()->query($sql);           
  
// if($res) {  
//      $data = array('errcode' => 0, 'msg' => '成功！');  
// } else {  
//      $data = array('errcode' => 1, 'msg' => '失败！');  
// }  
// exit( JSON($data));  
   }  
  
/*  
 * 从微信服务器获取图片流  
 */    
public function doWechatPic($serverId){//media_id=jlJs_iQIOA-TKLuhk4nCdPEdXnJ6paIeToO8vr-WUGvz05-6i5n498EzI232xSxn 
		 // f0gVq5v57ItPenIg254gx90ihyWukyVn37qyZPCOs24bQIB3UFZm7WZnxy8vB-CO    
       $media_id = $serverId;//提交过来的serverId即$media_id     
       Vendor('jssdk.jssdks');
       $a = new \jssdk();
	   $access_token = $a -> getAccessToken();
	   // pr($access_token);
       // $access_token    = $this->_get_wx_access_token_address( false );//获取access_token值  
        
        $pic_url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";    
        $filebody = file_get_contents($pic_url);//通过接口获取图片流    
         
        $filename = uniqid().'.jpg';            //定义图片名字及格式 
           // exit(JSON($filename ));  
          
        return $this->saveFile($filename, $filebody);    
}    
    
/*  
 * 定义文件路径，写入图片流  
 */    
public function saveFile($filename, $filecontent){    
  
    // $uploadROOT     = realpath(THINK_PATH.'../ceshi/');//定义保存路径       
    // $uploadPath     = $uploadROOT."/Upload/".date('Y-m-d')."/";//为方便管理图片 保存图片时 已时间作一层目录作区分  
    $uploadPath     = './Upload/' . date('Y-m-d').'/';//为方便管理图片 保存图片时 已时间作一层目录作区分  
    if(!file_exists($uploadPath)) mkdir($uploadPath,  0777);  
    $upload_dir = './Upload/' . date('Y-m-d'); //保存路径，以时间作目录分层    
    $savepath = $upload_dir.'/'.$filename;         
    if(file_put_contents($savepath, $filecontent)){//写入图片流生成图片    
        return $upload_dir."/".$filename;//返回图片路径    
    }else{
    	$this -> ajaxReturn('失败');    
        // exit(JSON('save failed'));    
    }    
        
} 
	
}


